<!DOCTYPE HTML>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../css/aui.css" />
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <title>AUI快速完成布局</title>
        <link rel="stylesheet" type="text/css" href="../../css/w3.css" />
        <link rel="stylesheet" type="text/css" href="../../css/aui.2.0.css" />
        <style type="text/css">
            body {
                background: #ffffff;
            }
            .bg-dark {
                background: #333333 !important;
            }
            .aui-bar-btn-item {
                background: #ffffff;
                border: none;
                border-bottom: 2px solid #ffffff;
            }
            .aui-bar-btn-item.aui-active {
                color: #039be5;
                background: #ffffff;
                border-bottom: 2px solid #039be5;
            }
            .nomore-tag {
                margin: 0 auto;
                text-align: center;
                width: auto;
                color: #333;
                padding: 20px 10px 10px 10px;
                font-size: 15px;
            }
            .title-bar {
                margin: 0 auto;
                text-align: left;
                width: auto;
                color: #333;
                padding: 3px 3px 2px 15px;
                font-size: 15px;
            }
        </style>
    </head>
    <body>
        <section class="aui-content">
            <ul class="aui-list aui-list-in">
                <li class="aui-list-header"></li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label-icon">
                            <i class="aui-iconfont aui-icon-date aui-text-info"></i>
                        </div>
                        <div class="aui-list-item-label" style="width:50%; margin-right: 1rem">
                            贷款提醒开关：
                        </div>
                        <div class="aui-list-item-input" style="width:50%;">
                            <input type="checkbox" id="alertCheck" class="aui-switch aui-switch-info aui-pull-right" checked onchange="toggleAlert()">
                        </div>
                    </div>
                </li>
            </ul>
        </section>
    </body>
    <script type="text/javascript" src="../../script/api.js" ></script>
    <script type="text/javascript" src="../../script/aui-tab.js" ></script>
    <script type="text/javascript" src="../../script/APICloud-rest-SHA1.js" ></script>
    <script type="text/javascript" src="../../script/aui-toast.js" ></script>
    <script type="text/javascript" src="../../script/aui-dialog.js" ></script>
    <script>
        var toast = new auiToast();
        var dialog = new auiDialog();
        function openWinFrame(winName, options) {
            var delay = 0;
            if (api.systemType != 'ios') {
                delay = 300;
            }
            api.openWin({
                name : winName,
                url : './' + winName + '.html',
                bounces : false,
                delay : delay,
                slidBackEnabled : true,
                vScrollBarEnabled : false,
                animation : {
                    type : "movein", //动画类型（详见动画类型常量）
                    subType : "from_left", //动画子类型（详见动画子类型常量）
                    duration : delay //动画过渡时间，默认300毫秒
                },
                pageParam : options,
            });
        }

        function joinQQQun() {
            var options = {
                url : 'http://shang.qq.com/wpa/qunwpa?idkey=5b1409d68e053892774a0ee40aba72c2970d099a7843ff37566fca27128f474f',
            };
            openWinFrame('loan_register_win', options);
        }

        function addWechatPub() {
            /*
             var wx = api.require('wx');
             wx.shareWebpage({
             apiKey : '',
             scene : 'timeline',
             title : '测试标题',
             description : '分享内容的描述',
             thumb : 'widget://image/my/app_icon.jpg',
             contentUrl : 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIyOTgyMjU4OQ==&scene=124#wechat_redirect'
             }, function(ret, err) {
             if (ret.status) {
             alert('分享成功');
             } else {
             alert(err.code);
             }
             });*/
        }

        function toggleAlert() {
            var model = api.require('model');
            model.config({
                appId : 'A6928874653494',
                appKey : '4B6C428C-24C2-F9E7-3175-F983922E8C8F',
                host : 'https://d.apicloud.com'
            });
            toast.loading({
                title : "请稍候",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            if (document.getElementById("alertCheck").checked) {
                //alert("checkbox is checked");
                model.updateById({
                    class : 'user',
                    id : $api.getStorage('userID'),
                    value : {
                        enableAlert : 1,
                    }
                }, function(ret, err) {
                    if (ret) {
                        toast.hide();
                        toast.success({
                            title : "提交成功！",
                            duration : 1000
                        });
                    } else {
                        toast.hide();
                        toast.fail({
                            title : "提交失败！",
                            duration : 1000
                        });
                    }
                });
            } else {
                //alert("checkbox is checked");
                model.updateById({
                    class : 'user',
                    id : $api.getStorage('userID'),
                    value : {
                        enableAlert : 0,
                    }
                }, function(ret, err) {
                    if (ret) {
                        toast.hide();
                        toast.success({
                            title : "提交成功！",
                            duration : 1000
                        });
                    } else {
                        toast.hide();
                        toast.fail({
                            title : "提交失败！",
                            duration : 1000
                        });
                    }
                });
            }
        }

        apiready = function() {
            api.parseTapmode();
            var model = api.require('model');
            model.config({
                appId : 'A6928874653494',
                appKey : '4B6C428C-24C2-F9E7-3175-F983922E8C8F',
                host : 'https://d.apicloud.com'
            });
            toast.loading({
                title : "加载中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var client = new Resource("A6928874653494", "4B6C428C-24C2-F9E7-3175-F983922E8C8F");
            var modelUser = client.Factory("user");
            if ( typeof ($api.getStorage('userID')) != "undefined") {
                modelUser.query({
                    filter : {
                        "limit" : 1,
                        "where" : {
                            "id" : $api.getStorage('userID')
                        }
                    }
                }, function(ret, err) {
                    if (err) {
                        //处理错误 err
                        alert(JSON.stringify(err));
                    } else {
                        //处理数据 ret
                        //alert(JSON.stringify(ret));
                        toast.hide();
                        if (ret.length) {
                            if (ret[0].enableAlert) {
                                document.getElementById("alertCheck").setAttribute('checked', 'true');
                            } else {
                                document.getElementById("alertCheck").removeAttribute('checked');
                            }
                        }
                    }
                });
            }
        }
    </script>
</html>